package com.yi.gostockgo.service.httpApi;

import com.yi.gostockgo.pojo.GoStock;
import com.yi.gostockgo.service.GoStockService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author ：liandyao
 * @description：爬取所有的股票信息
 * @date ：2020/4/29 9:53
 */
@Service
public class JsoupStock {

    static Logger logger = LoggerFactory.getLogger(JsoupStock.class);

    public static String URL = "http://quote.eastmoney.com/stock_list.html";

    @Autowired
    private GoStockService goStockService;

    public List<GoStock> getStock(){

        List<GoStock> stockList = new ArrayList<>();
        try {
            Document document = Jsoup.connect(URL).get();//建立连接并且得到HTML文档
            Elements list = document.select("#quotesearch").select("ul");//得到两个ul
            for(int i=0;i<list.size();i++){
                Element ele = list.get(i);
                String comp  = "";
                if(i==0){
                    comp="sh";
                }else if(i==1){
                    comp="sz";
                }
                //String comp  = ele.select("a").attr("name");//爬出证券代码,例如sh,sz
                //String comp = eleType.attr("name");
                Elements listStock = ele.select("li") ;
                for(Element e:listStock){
                    GoStock st = new GoStock();
                    st.setStockComp(comp) ;//爬出证券代码,例如sh,sz
                    Element node = e.child(e.childNodeSize()-1);
                    String text = node.text(); //得到股票代码和名称 //R003(201000)
                    st.setStockName(text.split("\\(")[0]);
                    st.setStockCode(text.split("\\(")[1].replaceAll("\\)",""));
                    st.setStockId(st.getStockComp()+st.getStockCode());
                    stockList.add(st);
                }


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stockList ;
    }


    /**
     * 项目启动时,加入@PostConstruct注解会自动运行这个方法.
     * @TODO 加入所有的股票信息
     */
    @Transactional
    //@PostConstruct
    public void runStart(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                logger.info("批量执行股票信息插入");
                List<GoStock> list = getStock();
                goStockService.saveBatch(list);

            }
        },1000,24* 60* 60 * 1000*7); //立刻执行,然后7天执行一次
    }


    public static void main(String[] args) {
        try {
            Document document = Jsoup.connect(URL).get();//建立连接并且得到HTML文档
            Elements list = document.select("#quotesearch").select("ul");//得到两个ul
            System.out.println(list.size());
            int i = 0;
            for(Element ele:list){

                Elements listStock = ele.select("li") ;
                for(Element e:listStock){
                    i++;
                    System.out.println(i+"    "+e.text());
                }



            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
